/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package management;

import entity.Manager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.JOptionPane;

/**
 *
 * @author Cucnth
 */
public class MngManager {

    PreparedStatement ps = null;
    public static String sql = "select Manager_Id, Manager_Name, Manager_UserName, Manager_Permission, Manager_Gender, Manager_Age, Manager_Phone, Manager_Address, Manager_Email, Manager_Status from Manager";

    public boolean addManager(Manager mng) {
        try {

            String sql1 = "insert into Manager values(?,?,?,?,?,?,?,?,?,?)";
            ps = management.MyConnection.getConnection().prepareStatement(sql1);
//            ps.setInt(1, mng.getId());
            ps.setString(1, mng.getFullName());
            ps.setString(2, mng.getUserName());
            ps.setString(3, mng.getPassWord());
            ps.setInt(4, mng.getPermission());
            ps.setString(5, mng.getGender());
            ps.setInt(6, mng.getAge());
            ps.setString(7, mng.getPhone());
            ps.setString(8, mng.getAddress());
            ps.setString(9, mng.getEmail());
            ps.setString(10, mng.getStatus());
            JOptionPane.showMessageDialog(null, "Add manager successfully !");
            return (ps.executeUpdate() == 1);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Add manager error !");
            ex.printStackTrace();
            return false;
        } finally {
            try {
                ps.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }

    public static boolean updateManager(String name, int permission, String gender, int age, String phone, String address, String email, String status, int id) {
        String sql2 = "update Manager set Manager_Name=?, Manager_Permission=?, Manager_Gender=?, Manager_Age=?, Manager_Phone=?, Manager_Address=?, Manager_Email=?, Manager_Status=? where Manager_Id=?";
        try {
            PreparedStatement ps = management.MyConnection.getConnection().prepareStatement(sql2);
            ps.setString(1, name);
            ps.setInt(2, permission);
            ps.setString(3, gender);
            ps.setInt(4, age);
            ps.setString(5, phone);
            ps.setString(6, address);
            ps.setString(7, email);
            ps.setString(8, status);
            ps.setInt(9, id);
            JOptionPane.showMessageDialog(null, "Edit manager successfully !");

            return (ps.executeUpdate() == 1);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Edit manager error!");
            ex.printStackTrace();
            return false;
        }
    }

    public static boolean updateMyInfo(String name, String pass, String gender, int age, String phone, String address, String userName) {
        String sql3 = "update Manager set Manager_Name=?, Manager_Pass=?, Manager_Gender=?, Manager_Age=?, Manager_Phone=?, Manager_Address=? where Manager_UserName=?";
        try {
            PreparedStatement ps = management.MyConnection.getConnection().prepareStatement(sql3);
            ps.setString(1, name);
            ps.setString(2, pass);
            ps.setString(3, gender);
            ps.setInt(4, age);
            ps.setString(5, phone);
            ps.setString(6, address);
            ps.setString(7, userName);
            JOptionPane.showMessageDialog(null, "Edit your's information successfully !");

            return (ps.executeUpdate() == 1);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Edit your's information error!");
            ex.printStackTrace();
            return false;
        }
    }
//    public int search(Manager mngSearch) {
//        try {
//            String sql3 = "select * from Manager";
//            ps = management.MyConnection.getConnection().prepareStatement(sql3);
//            ResultSet rs = ps.executeQuery();
//            while (rs.next()) {
//                if (rs.getString("Manager_UserName").equalsIgnoreCase(mngSearch.getUserName())) {
//                    int row = rs.getRow();
//                    rs.close();
//                    ps.close();
//                    return row - 1;
//                }
//            }
//            return -1;
//        } catch (SQLException ex) {
//            System.out.println(ex.toString());
//            return -1;
//        }
//    }
}
